<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class OAFiles extends Model
{
    protected $table = 'files';
    const FILE_TYPE_TEMP = 0;
    public static function getProjectFileList($work_id,$pid,$type)
    {
        $file_list =  self::query()
            ->when($pid,function ($query) use ($pid){
                if(is_array($pid)){
                    return $query->whereIn('pid',$pid);
                }else{
                    return $query->where('pid',$pid);
                }
            })
            ->when($work_id,function ($query) use ($work_id){
                return $query->where('work_id',$work_id);
            })
            ->where('type',$type)
            ->orderBy('id','desc')
            ->select(['id','name','link','ext','size','link as fileUrl'])->get()->toArray();
        if(empty($file_list)) return [];
        foreach ($file_list as &$file){
            if(!isset($file['link']) && empty($file['link'])) continue;
            $file['fileName'] = $file['name'];
            $file['ext'] = $file['ext'] ?? pathinfo($file['link'],PATHINFO_EXTENSION);
            $header = get_headers($file['link'],true);
            $file['size'] = $file['size'] ?? getSize($header['Content-Length']);
        }
        return $file_list;
    }

}
